#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main1()
{
    FILE *fp = fopen("log.txt", "a"); // append 追加
    if(fp == NULL)
    {
        perror("fopen");
        return 1;
    }
    
    char buffer[1024];
    const char *message = "hello file";
    int i = 0;
    while(i < 5)
    {
        snprintf(buffer, sizeof(buffer), "%s:%d\n", message, i);
        fputs(buffer, fp);
        i++;
    }

    fclose(fp);
    return 0;
}

int main2()
{
    printf("hello world\n");
    fputs("aaaa", stdout);
    fwrite("bbbb", 1, 4, stdout);
    fprintf(stdout, "cccc");
    return 0;
}


// 最后输出结果不是 12345
// 原因： 12345 是整数，但是显示器是个字符设备只认字符
// 解决：自己转化
int main3()
{
    int a = 12345;
    char buffer[1024];
    snprintf(buffer, sizeof(buffer), "%d", a);
    write(1, buffer, strlen(buffer));
    return 0;
}


int main()
{
    printf("hello printf\n");
    fprintf(stdout, "hello fprintf\n");

    const char *msg = "hello write\n";
    write(1, msg, strlen(msg));
    fork();
    return 0;
}





